package db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import model.Employee;
import model.EmployeeRole;

public class EmployeeRoleDAO {
	private static final String TABLE = "employee_role";	
	private static String onSqlError = "SQL statement fault";
		
	
	private static final String 
		ID_FIELD = "employee_role",			
		EMPLOYEE_FIELD = "employee_fk",
		EMPLOYEE_ROLE_TYPE_FIELD = "employee_role_type_fk",		
		ACTIVE_FIELD = "active";
	
	private static final String INSERT_STATEMENT_STRING = 
			"INSERT INTO "+TABLE+" " +
			"("+EMPLOYEE_FIELD+", "+EMPLOYEE_ROLE_TYPE_FIELD+", "+ACTIVE_FIELD+") " +
			"VALUES (?, ?, ?)";
	
	private static final String UPDATE_STATEMENT_STRING = 
			"UPDATE "+TABLE+" SET " 
			+EMPLOYEE_FIELD+" = ?, "+EMPLOYEE_ROLE_TYPE_FIELD+" = ?, "+ACTIVE_FIELD+" = ?"
			+" WHERE "+ID_FIELD+" = ?";
	
	private static final String DELETE_STATEMENT_STRING =
			"DELETE FROM "+TABLE+" WHERE "+ID_FIELD+" = ?";
	
	private static PreparedStatement insertStatement, updateStatement, deleteStatement;
	
	static{
		try {
			insertStatement = ImbiConnection.getConnection().prepareStatement(INSERT_STATEMENT_STRING);
			updateStatement = ImbiConnection.getConnection().prepareStatement(UPDATE_STATEMENT_STRING);
			deleteStatement = ImbiConnection.getConnection().prepareStatement(DELETE_STATEMENT_STRING);
		} catch (SQLException e) {			
			e.printStackTrace();
			System.exit(0);
		}
	}
	
	public EmployeeRole find(int id) {			
		try{				
			Statement st = ImbiConnection.getConnection().createStatement();
			st.executeQuery("SELECT * FROM "+TABLE+" WHERE "+ID_FIELD+" = "+id);
			EmployeeRole e = extractEmployeeRole(st.getResultSet()).get(0);			
			return e;
		} catch (SQLException | IndexOutOfBoundsException e){
				System.out.println(onSqlError);
				return null;
		}		
	}
	
	private ArrayList<EmployeeRole> extractEmployeeRole(ResultSet r) throws SQLException {
		ArrayList<EmployeeRole> list = new ArrayList<EmployeeRole>();
		while(r.next()){			
			EmployeeRole e = new EmployeeRole(
				r.getInt(1),
				r.getInt(2),
				r.getInt(3),
				"Y".equalsIgnoreCase(r.getString(4))
			);
			list.add(e);
		}
		return list;
	}	
	
	public ArrayList<EmployeeRole> findAllByEmployee(Employee emp) {
		try{				
			Statement st = ImbiConnection.getConnection().createStatement();
			st.executeQuery("SELECT * FROM "+TABLE+" WHERE "+EMPLOYEE_FIELD+" = "+emp.getId());								
			return extractEmployeeRole(st.getResultSet());			
		} catch (SQLException s){
				System.out.println(onSqlError);				
				return null;
		}	
	}	
	
	public void insert(EmployeeRole e){
		try{											
		    insertStatement.setInt(1, e.getEmployee());
		    insertStatement.setInt(2, e.getEmployeeRoleType());		    
		    insertStatement.setString(3, e.isActive() ? "Y" : "N");		    	    
		    insertStatement.executeUpdate();			
		} catch (SQLException s){
				System.out.println(onSqlError);								
		}		
	}
	
	public void update(EmployeeRole e){
		try{										
		    updateStatement.setInt(1, e.getEmployee());
		    updateStatement.setInt(2, e.getEmployeeRoleType());		    
		    updateStatement.setString(3, e.isActive() ? "Y" : "N");		        
		    updateStatement.setInt(4, e.getId());
		    updateStatement.executeUpdate();			
		} catch (SQLException s){
				System.out.println(onSqlError);									
		}			
	}
	
	public void delete(EmployeeRole e){
		try{										
		    deleteStatement.setInt(1, e.getId());		    
		    deleteStatement.executeUpdate();			
		} catch (SQLException s){
				System.out.println(onSqlError);					
		}			
	}		
}
